const app=getApp();
Page({
  data: {
    num: 0,
    noAddress: false,
    arr: [
      { id: 0, name: "小李 先生", phone: "12345654789", address: "山东济南天桥K88名泉广场" },
      { id: 1, name: "小李 先生", phone: "12345616789", address: "山东济南天桥K88名泉广场" },
      { id: 2, name: "小李 先生", phone: "12345675189", address: "山东济南天桥K88名泉广场" },
    ],
  },
  onLoad: function (options){
    this.getAddressallList();
    if (options.isorderpage == 1) {
      this.setData({
        isorderpage: 1
      })

    }
  },
  //提交订单页面选地址
  xunzeaddr: function (e) {
    var that = this;
    console.log(that.data.isorderpage, '是否订单页面')
    if (that.data.isorderpage == 1) {
      console.log(e, '地址')
      var addrid = e.currentTarget.dataset.id;
      var pages = getCurrentPages(); // 当前页面
      var beforePage = pages[pages.length - 2]; // 前一个页面
      wx.navigateBack({
        success: function () {
          beforePage.getxuanAddressList(addrid); // 执行前一个页面的onLoad方法
        }
      });
    } else {
      return;
    }

  },
  //地址
  //显示所有地址
  getAddressallList: function () {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Car/getAddressallList',
      data: {
        // opt: 'getAddress',
        userId: wx.getStorageSync('userId'),
      },

      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data != null) {
          that.setData({
            addressList: res.data,
          })
        }
      }
    })
  },
  //设置默认
  chooseAddress: function (e) {
    var that=this;
    var addrid = e.currentTarget.dataset.addrid;
    var addressList = that.data.addressList;
    // values = e.detail.value;
    for (var i = 0, lenI = addressList.length; i < lenI; ++i) {
      if (addressList[i].Id == addrid) {
        addressList[i].Default = 1;
      }else{
        addressList[i].Default = 0;
      }
    }

    that.setData({
      addressList: addressList
    });
    wx.request({
      url: app.globalData.apiUrl + 'Car/defaultAddress',
      data: {
        // opt: 'defaultAddress',
        id: addrid,
        userId: 1,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.status == 0) {
          wx.showToast({
            title: '成功!',//这里打印出登录成功
            icon: 'success',
            duration: 1000,
            success: function () {
             
            }
          })
        } else {
          return;
        }
      }
    })
  },
  //删除地址
  delAddress: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.addrid;
    wx.request({
      url: app.globalData.apiUrl + 'Car/deladdr',
      data: {
        // opt: 'deleteAddress',
        id: id,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.status == 0) {
          wx.showToast({
            title: '成功!',//这里打印出登录成功
            icon: 'success',
            duration: 1000,
            success: function () {
              that.getAddressallList();
            }
          })
        } else {
          wx.showToast({
            title: '提交失败!',
            icon: 'loading',
            duration: 1500
          })
        }
      }
    })
  },
  //修改地址
  toEdit: function (e) {
    var id = e.currentTarget.dataset.addrid;
    wx.navigateTo({
      url: 'addRess/addRess?id=' + id,
    })
  },
  addRess: function () {
    wx.navigateTo({
      url: 'addRess/addRess'
    })
  },
  // delAddress: function () {
  //   var that = this;
  //   wx.showModal({
  //     title: '提示',
  //     content: '确定删除该地址吗？',
  //     success: function (res) {
  //       if (res.confirm) {
  //         var index = that.data.num;
  //         var arr = that.data.arr;
  //         arr.splice(index, 1);
  //         that.setData({
  //           arr: arr
  //         })
  //       }
  //     }
  //   })

  // },
  
})